package lhb.kafka;


import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Properties;

public class kafkademo {

    public static void main(String[] args) {

        //创建kafka配置参数
        Properties properties = new Properties();
        //kafka集群
        properties.put("bootstrap.servers", "192.168.0.201:9092,192.168.0.202:9092,192.168.0.203:9092");
        //重试次数
        properties.put("acks", "all");
        //重试次数
        properties.put("retries", 1);
        //批次大小
        properties.put("batch.size", 16384);
        //等待时间
        properties.put("linger.ms", 1);
        //RecordAccumulator 缓冲区大小
        properties.put("buffer.memory", 33554432);
        properties.put("key.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer",
                "org.apache.kafka.common.serialization.StringSerializer");


        KafkaProducer<String, String> producer = new KafkaProducer<>(properties);

        for (int i = 0; i < 10; i++) {
            producer.send(new ProducerRecord<>("frist", "demo" + i), (metadata, exception) -> {
                if(exception == null){
                    System.out.println(metadata.partition()+"--"+metadata.topic());
                }
            });
        }
        //关闭
        producer.close();

    }
}
